package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.MapConstant;
import com.tbit.main.constant.SysConstant;
import com.tbit.main.pojo.Machine;
import com.tbit.main.pojo.MachineLastUse;
import com.tbit.main.pojo.Point;
import com.tbit.main.pojo.User;
import com.tbit.main.service.CalibrateService;
import com.tbit.main.service.MachineLastUseService;
import com.tbit.main.service.MachineService;
import com.tbit.main.service.UserService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 设备最后使用
 *
 * @author Leon
 * 2019年11月11日 下午1:37:42
 */
@RestController
@RequestMapping("/machineLastUse")
public class MachineLastUseController {
    @Autowired
    private MachineLastUseService machineLastUseService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private UserService userService;

    /**
     * 获取最后扫码人信息
     */
    @RequestMapping("/getInfo")
    public JsonResult getInfo(String token, Integer machineId, Integer mapType) {
        /* 校验 */
        Assert.notNull(machineId, "Machine.machineId.notNull");

        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        MachineLastUse mlu = machineLastUseService.getByMachineId(machineId);

        /**校准*/
        if (mlu != null) {
            Point point = calibrateService.commonCalibrate(new Point(mlu.getLo(), mlu.getLa()), MapConstant.MAP_ORI, mapType);
            mlu.setLoC(point.getX());
            mlu.setLaC(point.getY());

            /**获取用户码和设备编号*/
            Machine machine = machineService.getByMachineId(machineId);
            if (machine != null) {
                mlu.setUserCode(machine.getUserCode());
                mlu.setMachineNO(machine.getMachineNO());
            }
            //获取扫码用户信息
            if (Objects.nonNull(mlu.getUserId())) {
                User user = userService.getByUserId(mlu.getUserId());
                mlu.setUserName(Objects.nonNull(user) ? user.getName() : null);
            }
        }

        return JsonResult.succ(mlu);
    }

}